package org.java.user.utils;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.List;

/**
 * @author WHQ
 * @version 1.0
 * @date 2022-3-22 0022 下午 2:47
 * hibernate-validator数据校验框架抛出绑定异常
 */
@ControllerAdvice
@Slf4j
public class UserExceptionHandler {
    private static final Logger LOGGER=LoggerFactory.getLogger(UserExceptionHandler.class);
    @ExceptionHandler(BindException.class)
    public ResponseEntity exceptionHandler(BindException e){
        System.out.println("进入异常处理");
        FieldError fieldError = e.getBindingResult().getFieldError();
        String field = fieldError.getField();
        LOGGER.info("异常字段      :{}",field);
        return ResponseEntity.badRequest().body(field);
    }
}
